file = 'zen.txt'
# print('1.使用read读取文件内容，但这种打开方法不好！')
# f = open(file, 'r')
# print(f.read())
# f.close()

# print('2. 只读取部分内容')
# f = open(file, 'r')
# print(f.read(5))
# print(f.read(8))
# f.close()

# print('3. 更好的读取内容')
# with open(file, 'r') as f:
#     print(f.read())

print('4. 用readlines按行读取')
with open(file, 'r') as f:
    lines = f.readlines()
    print(f'共有#{len(lines)}行内容')
    print(lines)

print('5. 用readline - 读取前5行')
with open(file, 'r') as f:
    for i in range(0,5):
        print(f.readline(), end='')

print('6. 按行读取-直接循环文件')
with open(file, 'r') as f:
    for line in f:
        print(line, end='')